package com.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.Food;
import com.bean.FoodMaterial;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.FoodMaterialService;
import com.service.FoodService;
import com.util.ChineseCastPinyinUtil;

/**
 * @author author : 张鹏
 * @date createTime：2018年1月10日 下午10:46:47
 * @Description: 该类的功能描述
 */
@Controller
@RequestMapping("food")
public class FoodController {
	@Autowired
	private FoodService foodService;
	@Autowired
	private FoodMaterialService foodMaterialService;

	Food food;

	/**
	 * 分页的显示数量
	 */
	int pageSize = 6;

	/**
	 * 查询所有菜品
	 * 
	 * @return
	 */
	@RequestMapping("getFoodByPage")
	public String getFoodByPage(
			@RequestParam(required = true, defaultValue = "1") int page,
			HttpServletRequest request) {
		if (page < 1) {
			page = 1;
		}
		PageHelper.startPage(page, pageSize);
		List<Food> list = foodService.getAllFood();
		// 取分页信息
		PageInfo<Food> pageInfo = new PageInfo<Food>(list);

		System.out.println(pageInfo);

		request.setAttribute("foodList", list);
		request.setAttribute("page", pageInfo);
		return "foodManage";
	}

	/**
	 * 查询
	 * 
	 * @param page
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("searchFoodByPage")
	public String searchFoodByPage(
			@RequestParam(required = true, defaultValue = "1") int page,
			String searchWords, HttpServletRequest request) throws UnsupportedEncodingException {
		if (page < 1) {
			page = 1;
		}
		PageHelper.startPage(page, pageSize);
		if (searchWords!=null) {
			searchWords=java.net.URLDecoder.decode(searchWords , "UTF-8");
		}else {
			searchWords="";
		}
		List<Food> list = foodService.getFoodByFoodNameOrPinyin(searchWords);
		// 取分页信息
		PageInfo<Food> pageInfo = new PageInfo<Food>(list);

		System.out.println(pageInfo);
		System.out.println(searchWords);

		request.setAttribute("foodList", list);
		request.setAttribute("page", pageInfo);
		request.setAttribute("searchWords", searchWords);
		return "foodManage";
	}

	/**
	 * 创建新的菜品
	 * 
	 * @param FoodName
	 * @return
	 */
	@RequestMapping("createNewFood")
	@ResponseBody
	public String createNewFood(String foodName, int classificationId,
			int unitId, Double productPrice, Double sellPrice, String remark,
			String foodMaterialList) {
		food = new Food();
		food.setFoodName(foodName);
		food.setClassificationId(classificationId);
		food.setUnitId(unitId);
		food.setProductPrice(productPrice);
		food.setSellPrice(sellPrice);
		food.setRemark(remark);
		// 设置菜品名的拼音
		food.setPinyin(ChineseCastPinyinUtil.getPinYinHeadChar(food
				.getFoodName()));
		// 插入菜单表
		int result = foodService.createNewFood(food);

		// 得到刚插入的数据的主键 foodId
		int foodId = food.getFoodId();

		// 处理菜品对应的原料
		if (!"".equals(foodMaterialList) && foodMaterialList != null
				&& !"[]".equals(foodMaterialList)) {
			FoodMaterial foodMaterial;
			JSONArray data = JSONArray.fromObject(foodMaterialList);
			JSONObject jobj;
			for (int i = 0; i < data.size(); i++) {
				jobj = (JSONObject) data.get(i);
				foodMaterial = new FoodMaterial();
				foodMaterial.setFoodId(foodId);
				foodMaterial.setMaterialId((int) jobj.get("materialId"));
				foodMaterial.setMaterialQuantity(Double.valueOf((String) jobj
						.get("materialQuantity")));

				foodMaterialService.insertFoodMaterial(foodMaterial);
			}
		}

		return result + "";
	}

	/**
	 * 编辑菜品
	 * 
	 * @param FoodId
	 * @param FoodName
	 * @return
	 */
	@RequestMapping("editFood")
	@ResponseBody
	public String editFood(int foodId, String foodName, int classificationId,
			int unitId, double productPrice, double sellPrice,
			String foodMaterialList) {

		food = new Food();
		food.setFoodId(foodId);
		food.setFoodName(foodName);
		// 设置菜品名的拼音
		food.setPinyin(ChineseCastPinyinUtil.getPinYinHeadChar(food
				.getFoodName()));
		food.setClassificationId(classificationId);
		food.setUnitId(unitId);
		food.setProductPrice(productPrice);
		food.setSellPrice(sellPrice);

		// 删除原本的原料
		foodMaterialService.deleteByFoodId(foodId);

		// 插入新选择的原料
		if (!"".equals(foodMaterialList) && foodMaterialList != null
				&& !"[]".equals(foodMaterialList)) {
			FoodMaterial foodMaterial;
			JSONArray data = JSONArray.fromObject(foodMaterialList);
			JSONObject jobj;
			for (int i = 0; i < data.size(); i++) {
				jobj = (JSONObject) data.get(i);
				foodMaterial = new FoodMaterial();

				foodMaterial.setFoodId(foodId);
				foodMaterial.setMaterialId((int) jobj.get("materialId"));
				foodMaterial.setMaterialQuantity(Double.valueOf(jobj.get(
						"materialQuantity").toString()));

				// 插入数据库
				foodMaterialService.insertFoodMaterial(foodMaterial);
			}
		}
		return foodService.editFood(food) + "";
	}

	/**
	 * 删除菜品
	 * 
	 * @param FoodId
	 * @return
	 */
	@RequestMapping("deleteFood")
	@ResponseBody
	public String deleteFood(int foodId) {
		System.out.println("删除" + foodId);
		return foodService.deleteFood(foodId) + "";
	}

	@RequestMapping("getFoodByPrimaryKeyAjax")
	@ResponseBody
	public Food getFoodByPrimaryKeyAjax(int foodId) {
		return foodService.getFoodByPrimaryKey(foodId);
	}

	@RequestMapping("getFoodByPrimaryKey")
	public String getFoodByPrimaryKey(int foodId, HttpServletRequest request) {
		// 输出菜品的详情
		request.setAttribute("food", foodService.getFoodByPrimaryKey(foodId));
		// 输出菜品对应的原料的列表
		request.setAttribute(
				"foodMaterialList",
				JSONArray.fromObject(
						foodMaterialService.getFoodMaterialByFoodId(foodId))
						.toString());
		return "editFood";
	}

	/**
	 * 根据主键查询
	 * 
	 * @param foodId
	 * @param returnTo
	 *            返回的jsp的名称
	 * @param request
	 * @return
	 */
	@RequestMapping("getFoodByPrimaryKeyNoMaterialReturnTo")
	public String getFoodByPrimaryKeyReturnTo(int foodId, String returnTo,
			HttpServletRequest request) {
		// 输出菜品的详情
		request.setAttribute("food", foodService.getFoodByPrimaryKey(foodId));
		return returnTo;
	}

	@RequestMapping("getFoodByClassificationIdAjax")
	@ResponseBody
	public List<Food> getFoodByClassificationIdAjax(int classificationId) {
		return foodService.getFoodByClassificationId(classificationId);
	}

	/**
	 * 通过菜品名称或pinyin 搜索
	 * 
	 * @param searchWords
	 * @return
	 */
	@RequestMapping("getFoodByFoodNameOrPinyinAjax")
	@ResponseBody
	public List<Food> getFoodByFoodNameOrPinyinAjax(String searchWords) {
		System.out.println(searchWords);
		return foodService.getFoodByFoodNameOrPinyin(searchWords);
	}

}
